const _unescape = (value: string) => {
  const text = String(value), len = text.length
  if (!len) {
    return text
  }
  let buffer = '', k = 0
  while (k < len) {
    let char = text[k]
    if (char === '%') {
      let chars = k <= (len - 6) && text[k + 1] === 'u' ? text.substring(k + 2, k + 6) : k <= (len - 3) ? text.substring(k + 1, k + 3) : ''
      if (!/^[0-9A-F]+$/i.test(chars)) chars = ''
      if (chars.length === 4) {
        char = String.fromCharCode(parseInt(chars, 16))
        k += 5
      } else if (chars.length === 2) {
        char = String.fromCharCode(parseInt('00' + chars, 16))
        k += 2
      }
    }
    buffer += char
    k += 1
  }
  return buffer
}

export default _unescape